package com.example.dotavideo.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.example.dotavideo.bean.TypeBean;
import com.example.dotavideo.bean.VideoBean;

public class JsonUtil {


  public static ArrayList<TypeBean> wrapTypeBeanList(String json) {

    ArrayList<TypeBean> list = new ArrayList<TypeBean>();

    TypeBean typeBean;

    try {
      JSONObject result = new JSONObject(json);
      String code = result.getString("code");
      if ("0".equals(code)) {

        JSONArray typeList = new JSONArray(result.getString("modules"));

        for (int i = 0; i < typeList.length(); i++) {

          typeBean = new TypeBean();
          JSONObject bean = (JSONObject) typeList.opt(i);
          String name = bean.getString("name");
          String id = bean.getString("id");

          typeBean.setName(name);
          typeBean.setTypeId(id);

          String childJson = bean.optString("child");

          if (childJson != null && !"".equals(childJson)) {

            JSONArray childList = new JSONArray(childJson);
            Map<String, String> map = new HashMap<String, String>();

            for (int j = 0; j < childList.length(); j++) {
              JSONObject child = (JSONObject) childList.opt(j);
              map.put(child.getString("name"), child.getString("id"));

              Log.e("TAG", child.getString("name"));
            }

            typeBean.setChildType(map);

          }
          list.add(typeBean);
        }
        return list;
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }

  public static ArrayList<VideoBean> wrapVideoBeanList(String json) {

    ArrayList<VideoBean> list = new ArrayList<VideoBean>();
    VideoBean videoBean = null;
    try {
      JSONObject result;
      result = new JSONObject(json);

      String code = result.getString("code");
      int totalPage = result.getInt("totalPage");
      
      if ("0".equals(code)) {

        JSONArray videoList = new JSONArray(result.optString("video"));

        for (int i = 0; i < videoList.length(); i++) {

          JSONObject bean = (JSONObject) videoList.opt(i);
          String title = bean.getString("title");
          String detail = bean.getString("summary");
          String date = bean.getString("time");
          String urlImage = bean.getString("picture");
          String urlFlv = bean.getString("flv");
          String urlHd = bean.getString("hd2");
          String urlContent = bean.getString("content");
          String videoId = bean.getString("id");
          String typeId = bean.getString("id");
          videoBean =
              new VideoBean(title, detail, date, urlImage, urlHd, urlFlv, urlContent, videoId,
                  typeId,totalPage);

          list.add(videoBean);
          
        }
        return list;
      }
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }



}














